package edu.virginia.cs4501.team22;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Application;

import com.google.android.maps.GeoPoint;

import edu.virginia.cs4501.team22.objects.Event;

public class ClassFinderApp extends Application {

	private static ClassFinderApp instance;

	private static int schedulePointer;//temp solution

	private static ArrayList<Event> classSchedule = null;
	private static HashMap<String, GeoPoint> buildingLocationCache = null;

	public boolean onSchedule;
	public Event nowEvent;

	@Override
	public void onCreate() {
		super.onCreate();
		instance = this;
		resolveSchedule();
	}

	public void resolveSchedule() {
		classSchedule = new ArrayList<Event>();
		classSchedule.add(new Event("Me time", "Alderman Library", "00"));
		classSchedule.add(new Event("CS4501", "Olsson", "011"));
		classSchedule.add(new Event("ENLT2526", "Cabell", "338"));

		schedulePointer = 0;
		if(classSchedule.size() <= 0)
			onSchedule = false;
		else {
			onSchedule = true;
			setEventNow(classSchedule.get(schedulePointer), true);
		}
	}

	public void incrementSchedule() throws IndexOutOfBoundsException {
		getOnSchedule();
		if (schedulePointer == classSchedule.size())
			throw new IndexOutOfBoundsException();
		schedulePointer++;
		setEventNow(classSchedule.get(schedulePointer), true);
	}

	public void decrementSchedule() throws IndexOutOfBoundsException {
		getOnSchedule();
		if (schedulePointer == 0)
			throw new IndexOutOfBoundsException();
		schedulePointer--;
		setEventNow(classSchedule.get(schedulePointer), true);
	}

	@Override
	public void onTerminate() {
		super.onTerminate();
	}

	public ArrayList<Event> getSchedule() {
		return classSchedule;
	}

	public void addClass(Event newClass) {
		classSchedule.add(newClass);
	}

	public Event getEventNow() {
		if (!onSchedule)
			return (nowEvent==null ? new Event("Me time", "Alderman Library", "00") : nowEvent);
		if (classSchedule.size() >0)
			return classSchedule.get(schedulePointer);
		else
			return new Event("Me time", "Alderman Library", "00");
	}

	public void getOnSchedule() {
		if (onSchedule)
			return ;
		if (classSchedule.size() == 0)
			addClass(new Event("Me time", "Alderman Library", "00"));
		schedulePointer = 0;
		onSchedule = true;

	}

	public void setEventNow(Event newEvent, boolean preserveSchedule) {
		nowEvent = newEvent;
		if (!preserveSchedule)
			onSchedule = preserveSchedule;//false
	}
}
